﻿using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.ConnectionStrings;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.Entities.ESGRatings;
using Sustainalytics.ESG.Entities;
using System.Collections.Generic;
using System.Configuration;
using CompanyWeights = Sustainalytics.ESG.Entities.CompanyWeights;

namespace Sustainalytics.Controversies.BusinessLogic
{
    public static class PlainBlFactory
    {
        private static readonly Dictionary<string, ConnectionStringType> _typesAndConnStrings = new Dictionary
            <string, ConnectionStringType>()
        {
            {typeof (RPControversyIndicator).Name, ConnectionStringType.ClientDW},
            {typeof (Incident).Name, ConnectionStringType.ClientDW},
            {typeof (EventIndicator).Name, ConnectionStringType.ClientDW},
            {typeof (ComparisonGroupControversy).Name, ConnectionStringType.ClientDW},
            {typeof (CorporateData).Name, ConnectionStringType.ClientDW},
            {typeof (IndicatorTemplate).Name, ConnectionStringType.ClientDW},
            {typeof (ESGProfile).Name, ConnectionStringType.ClientPlatform},
            {typeof (ReferenceUniverse).Name, ConnectionStringType.ClientDW},
            {typeof (Account).Name, ConnectionStringType.ClientPlatform},
            {typeof (WeightMatrix).Name, ConnectionStringType.ClientDW},
            {typeof (CompanyWeights).Name, ConnectionStringType.ClientDW},
            {typeof (AccountIpAuthentication).Name, ConnectionStringType.ClientPlatform},
            {typeof (User).Name, ConnectionStringType.ClientPlatform},
            {typeof (ClientUser).Name, ConnectionStringType.ClientPlatform},
            {typeof (RPESGResearchState).Name, ConnectionStringType.ClientDW},
            {typeof (TemplateCriteria).Name, ConnectionStringType.ClientPlatform},
            {typeof (CriteriaLevel3).Name, ConnectionStringType.ClientPlatform},
            {typeof (FilterCriteria).Name,ConnectionStringType.ClientPlatform},
            {typeof (SustainalyticsIndustryGroup).Name,ConnectionStringType.ClientDW},
            {typeof (PeerGroup).Name,ConnectionStringType.ClientDW},
        };

        public static IPlainBl<T> GetInstance<T>() where T : IEntity
        {
            var collectionName = typeof (T).Name;

            var repcol = new RepositoryCollection<T>(GetConnectionString(_typesAndConnStrings[collectionName]), collectionName);
            return new PlainMongoBl<T>(repcol) as IPlainBl<T>;
        }

        public static IPlainBl<T> GetInstance<T>(string connectionString) where T : IEntity
        {
            var collectionName = typeof(T).Name;

            var repcol = new RepositoryCollection<T>(connectionString, collectionName);
            return new PlainMongoBl<T>(repcol) as IPlainBl<T>;
        }

        public static string GetConnectionString(ConnectionStringType tip)
        {
            // and use try parse
            var connectionString = ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString;
            
            if (tip == ConnectionStringType.ClientPlatform)
            {
                return connectionString;
            }

            if (tip == ConnectionStringType.TemporaryDataSet)
            {
                return ConfigurationManager.ConnectionStrings["RPHistoricalDataSet"].ConnectionString;
            }

            var connectionStringsManager =
                ConnectionStringsManager.GetInstance(ConnectionStringsRepository.GetInstance(connectionString));
            return connectionStringsManager.GetConnectionString(tip);
        }
    }
}
